<?php

class simple_image {

    var $image;
    var $imageFileName;
    var $image_type;
    var $image_info;

    function __construct($filename) {
        $this->imageFileName=$filename;
        $this->image_info = $image_info = getimagesize($filename);
        $this->image_type = $image_info[2];
        if ($this->image_type == IMAGETYPE_JPEG) {
            $this->image = imagecreatefromjpeg($filename);
        } elseif ($this->image_type == IMAGETYPE_GIF) {
            $this->image = imagecreatefromgif($filename);
        } elseif ($this->image_type == IMAGETYPE_PNG) {
            $this->image = imagecreatefrompng($filename);
        }
    }

    function save($filename, $image_type = IMAGETYPE_JPEG, $compression = 90, $permissions = null) {
        $result=false;
        imageconvolution($this->image, array(array(-1,-1,-1),array(-1,16,-1),array(-1,-1,-1)), 8, 0); //subtle sharpen
        if ($image_type == IMAGETYPE_JPEG) {
            $result=imagejpeg($this->image, $filename, $compression);
        } elseif ($image_type == IMAGETYPE_GIF) {
            $result=imagegif($this->image, $filename);
        } elseif ($image_type == IMAGETYPE_PNG) {
            $result=imagepng($this->image, $filename);
        }
        if ($permissions != null) {
            chmod($filename, $permissions);
        }
        return $result;
    }

    function output($image_type = IMAGETYPE_JPEG) {
        imageconvolution($this->image, array(array(-1,-1,-1),array(-1,16,-1),array(-1,-1,-1)), 8, 0); //subtle sharpen
        if ($image_type == IMAGETYPE_JPEG) {
            imagejpeg($this->image);
        } elseif ($image_type == IMAGETYPE_GIF) {
            imagegif($this->image);
        } elseif ($image_type == IMAGETYPE_PNG) {
            imagepng($this->image);
        }
    }

    function getWidth() {
        return imagesx($this->image);
    }

    function getHeight() {
        return imagesy($this->image);
    }

    function resizeToHeight($height) {
        $ratio = $height / $this->getHeight();
        $width = $this->getWidth() * $ratio;
        $this->resize($width, $height);
    }

    function resizeToWidth($width) {
        $ratio = $width / $this->getWidth();
        $height = $this->getheight() * $ratio;
        $this->resize($width, $height);
    }

    function scale($scale) {
        $width = $this->getWidth() * $scale / 100;
        $height = $this->getheight() * $scale / 100;
        $this->resize($width, $height);
    }

    function resize($width, $height) {
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->image = $new_image;
    }

    function fitToBox($box = 200){
        $scale = min($box / $this->getWidth(), $box / $this->getHeight(), 1);
        $this->scale($scale*100);
    }

    function crop($startX, $startY, $width , $height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, $startX, $startY, $width, $height, $width, $height);
      $this->image = $new_image;
    }
    
    function resizeCrop($startX, $startY, $width , $height, $isEditingCoverImage, $isEditingAvatar){
        if($isEditingCoverImage)
            $this->resizeToWidth($width);
        
        if($isEditingAvatar){
            if($this->getHeight() > $this->getWidth()){
                $isResizeToWidth = true; $isResizeToHeight = false;
            }elseif($this->getHeight() < $this->getWidth()) {
                $isResizeToWidth = false; $isResizeToHeight = true;
            }else{
                $isResizeToHeight = false; $isResizeToWidth = false;
                $startY = 0; $startX = 0;
            }

            if($isResizeToWidth){ 
                $gap = $this->getHeight() - $this->getWidth();
                $widthRatio = $this->getWidth() / $gap;
                $heightRatio = $this->getHeight() / $gap;
                
                if(($widthRatio - $heightRatio) == 1)
                    $toDivide = 10;
                else
                    $toDivide = 5;
                
                $startY = ($gap / $toDivide); $startX = 0;
                $this->resizeToWidth($width);
            }

            if($isResizeToHeight){
                $gap = $this->getWidth() - $this->getHeight();
                $widthRatio = $this->getWidth() / $gap;
                $heightRatio = $this->getHeight() / $gap;
                
                if(($widthRatio - $heightRatio) == 1)
                    $toDivide = 10;
                else
                    $toDivide = 5;
                
                $startX = ($gap / $toDivide); $startY = 0;
                $this->resizeToHeight($height);
            }
        }   
        $new_image = imagecreatetruecolor($width, $height);
        imagecopyresampled($new_image, $this->image, 0, 0, $startX, $startY, $width, $height, $width, $height);
        $this->image = $new_image;
    }
    
}